//
//  MTOpenRedpackInputView.swift
//  MT
//
//  Created by ooops on 2019/12/23.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
import AutoInch
import MTCategory

protocol MTOpenRedpackInputViewDelegate: NSObjectProtocol {
    //func openRedpack(_ view: MTOpenRedpackInputView, model: MTOpenRedpackModel?)
    func openRedpack(_ view: MTOpenRedpackInputView, model: MTOpenRedpackModel?, msgId: String, type: MTRedpacketType, callback: (() -> Void)?)
    func openRedpackDetail(_ view: MTOpenRedpackInputView, model: MTOpenRedpackModel?)
}

class MTOpenRedpackInputView: UIView {

    weak var delegate: MTOpenRedpackInputViewDelegate?
    
    var msgId: String = ""
    
    var type: MTRedpacketType = .default
    
    var callback: (() -> Void)?
    
    var openRedpackModel: MTOpenRedpackModel? {
        didSet {
            if let nickName = openRedpackModel!.result?.sendUserNickname {
                fromUserNameLabel.text = "\(nickName)的红包"
            }
            
            messageLabel.text = openRedpackModel?.result?.remark
            
            if let status: MTOpenRedpackStatus = openRedpackModel?.result?.status {
                switch status {
                case .normal:
                    subMessageLabel.isHidden = true
                    openDetailBtn.isHidden = true
                    openBackgroundV.isHidden = false
                    openBtn.isHidden = false
                case .invalid:
                    subMessageLabel.isHidden = false
                    subMessageLabel.text = "红包已超过24小时, 不能领取"
                    openDetailBtn.isHidden = true
                    openBackgroundV.isHidden = true
                    openBtn.isHidden = true
                case .clearance:
                    subMessageLabel.isHidden = false
                    subMessageLabel.text = "手慢了, 红包抢完了"
                    openDetailBtn.isHidden = true
                    openBackgroundV.isHidden = true
                    openBtn.isHidden = true
                case .failed:
                    subMessageLabel.isHidden = true
                    openDetailBtn.isHidden = true
                    openBackgroundV.isHidden = false
                    openBtn.isHidden = false
                default:
                    break
                }
            }
            /// 红包发送者头像
            avatorImgView.kf.setImage(with: URL(string: openRedpackModel?.result?.sendUserHeadHref ?? ""))
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.layer.cornerRadius = 10
        self.clipsToBounds = true
        self.backgroundColor = UIColor(hex: 0xF35C4B)
        setupUI()
        makeConstraint()
    }
    
    private func setupUI() {
        addSubview(circleImgView)
        addSubview(fromUserNameLabel)
        addSubview(avatorImgView)
        addSubview(messageLabel)
        addSubview(subMessageLabel)
        addSubview(openBackgroundV)
        openBackgroundV.addSubview(openBtn)
        addSubview(openDetailBtn)
    }
    
    private func makeConstraint() {
        circleImgView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(avatorImgView.snp.bottom).offset(-6)
            make.left.equalToSuperview().offset(-40)
            make.right.equalToSuperview().offset(40)
            make.height.equalTo(circleImgView)
        }
        
        fromUserNameLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(61.auto())
            make.left.equalToSuperview().offset(10.auto())
            make.right.equalToSuperview().offset(-10.auto())
        }
        
        avatorImgView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(fromUserNameLabel.snp.bottom).offset(9.auto())
            make.width.height.equalTo(44.auto())
        }
        
        messageLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(avatorImgView.snp.bottom).offset(40.auto())
            make.left.right.equalTo(fromUserNameLabel)
        }
        
        subMessageLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(messageLabel.snp.bottom).offset(11.auto())
        }
        
        openBackgroundV.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(messageLabel.snp.bottom).offset(16.auto())
            make.width.height.equalTo(82.auto())
        }
        
        openBtn.snp.makeConstraints { (make) in
            make.center.equalTo(openBackgroundV)
            make.width.height.equalTo(74.auto())
        }
        
        openDetailBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-21.auto())
        }
        
    }
    
    lazy var circleImgView: UIImageView = {
        let imgV = UIImageView()
        let image = UIImage(color: UIColor(hex: 0xFD4146), size: CGSize(width: ScreenWidth, height: ScreenWidth))?.roundCorners(cornerRadius: ScreenWidth * 0.5)?.innerShadowedImage(shadowColor: UIColor(hex: 0xFD4146, alpha: 1), radius: 10)
        imgV.image = image
        return imgV
    }()
    
    lazy var fromUserNameLabel: UILabel = {
        let l = UILabel()
        l.textColor = UIColor(hex: 0xFFFFFF)
        l.font = PingFangSCRegularFont(14.auto())
        l.text = ""
        l.textAlignment = .center
        return l
    }()
    
    lazy var avatorImgView: UIImageView = {
        let imgV = UIImageView()
        imgV.layer.cornerRadius = 22.auto()
        imgV.layer.masksToBounds = true
        imgV.backgroundColor = .white
        imgV.contentMode = .scaleAspectFill
        return imgV
    }()
    
    
    lazy var messageLabel: UILabel = {
        let l = UILabel()
        l.text = ""
        l.textAlignment = .center
        l.textColor = MainYellowColor
        l.font = PingFangSCSemiboldFont(22.auto())
        return l
    }()
    
    lazy var subMessageLabel: UILabel = {
        let l = UILabel()
        l.text = ""
        l.textAlignment = .center
        l.textColor = .white
        l.font = PingFangSCRegularFont(18.auto())
        return l
    }()
    
    lazy var openBackgroundV: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(hex: 0xF01963)
        v.layer.cornerRadius = 41.auto()
        return v
    }()
    
    lazy var openBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("打开", for: .normal)
        btn.backgroundColor = MainYellowColor
        btn.setTitleColor(UIColor.red, for: .normal)
        btn.titleLabel?.font = PingFangSCRegularFont(22.auto())
        btn.layer.cornerRadius = 37.auto()
        btn.layer.masksToBounds = true
        btn.addTarget(self, action: #selector(MTOpenRedpackInputView.openRedpack), for: .touchUpInside)
        return btn
    }()
    
    lazy var openDetailBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("查看领取详情 >", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = PingFangSCRegularFont(14.auto())
        btn.addTarget(self, action: #selector(MTOpenRedpackInputView.openRedpackDetail), for: .touchUpInside)
        return btn
    }()
    
    @objc private func openRedpack() {
        self.delegate?.openRedpack(self, model: self.openRedpackModel, msgId: msgId, type: type, callback: callback)
    }
    
    @objc private func openRedpackDetail() {
        self.delegate?.openRedpackDetail(self, model: self.openRedpackModel)
    }
    
    override func sizeThatFits(_ size: CGSize) -> CGSize {
        super.sizeThatFits(size)
        return CGSize(width: 269.auto(), height: 389.auto())
    }
    
    
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

}
